package com.hyz.login.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyz.login.model.Hospitalprice;
import com.hyz.login.service.HospitalpriceService;
import com.hyz.login.service.PharmacyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zwq
 * @since 2022-11-30
 */
@RestController
@RequestMapping("/page")
public class HospitalpriceController {

    @Autowired
    private HospitalpriceService hospitalpriceService;
    @Autowired
    private PharmacyService pharmacyService;

    @GetMapping("/liao/selDrugs")
    public String selDrugs(Integer registerid,Integer type,
                          @RequestParam(required = false,defaultValue = "1")Integer page,
                          @RequestParam(required = false,defaultValue = "5")Integer limit){
        IPage<Hospitalprice> iPage = hospitalpriceService.findByRegId(registerid,type,page,limit);
        return "{\"msg\":\"\",\"code\":0,\"count\":"+iPage.getTotal()+",\"data\":"+ JSON.toJSONString(iPage.getRecords())+"}";
    }

    @PostMapping("/liao/delDrug")
    public String delDrugs(Integer hospitalpriceid,String durgname,Integer durgnum){
        int ret = hospitalpriceService.delDrugs(hospitalpriceid);
        if (ret>0){
            pharmacyService.addDrugstorenum(durgname,durgnum);
            return "删除成功";
        }
        return "删除失败";
    }

    @PostMapping("/liao/addDrug")
    public String addDrug(Integer registerid,Hospitalprice hospitalprice,Integer pharmacyId){
        hospitalprice.setRegisterId(registerid);
        hospitalprice.setState(0);
        hospitalprice.setRepicetotal(hospitalprice.getDurgnum()*hospitalprice.getRepiceprice());
        int ret = hospitalpriceService.addDrug(hospitalprice);
        if (ret>0){
            pharmacyService.modifyDrugstorenum(hospitalprice.getDurgname(),hospitalprice.getDurgnum());
            return "添加成功";
        }
        return "添加失败";
    }
}

